<?php

class Adsent_save_modul  extends CI_Model {
    public $select;
    public $id_phone;

    public function __construct() {
        // Call the Model constructor
        parent::__construct();
        $this -> load -> library('session');
        $this -> id_phone = $this -> session -> userdata('id_phone');
    }

    public function metro($id = 0) {
        $DB2 = $this -> load -> database('adsent', TRUE, TRUE);
        $select = "SELECT * FROM `metro` where `id` = '" . $id . "'  LIMIT 1 ;";
        $query = $DB2 -> query($select);

        if ($query -> num_rows() > 0) {
            $row = $query -> row();
            return $row -> name;
        } else {

            return 0;

        }
    }

    /**
     * Добавление нового объявления в базу
     *
     * @param array $data массим с данными для добавление в базу
     * @return int $id уникальный идентификатор объявления
     * @author
     */

    public function rent_flat($data) {

        $DB2 = $this -> load -> database('adsent', TRUE, TRUE);
        $data['datetime'] = date("Y-m-d H:i:s", time());
        $data['id_phone'] = $this -> id_phone;
        $data['act'] = '1';
        $data['name_type'] = 'rflat';

        if ($data['city_id'] == '7700000000000' || $data['city_id'] == '7800000000000') {
            $data['region_id'] = $data['city_id'];
        }

        // узнаём точное местоположение объёкта
        if (isset($data["yandex_point"])) {
            if (strlen($data["yandex_point"]) < 10) {
                $data["yandex_point"] = $this -> yandex($data["adress_name"]);
            } else {
                $data["yandex_point"] = $data["yandex_point"];
            };
        } else {

            $data["yandex_point"] = $this -> yandex($data["adress_name"]);

        }

        $select = "INSERT INTO `rflat` 
( `newtime`,
`points`, 
`root`, 
`kitchen`, 
`area`, 
`l_area`, 
`t_area`, 
`adress_name`, 
`region_names`, 
`region_id`, 
`city_name`, 
`city_id`, 
`street_name`,
`street_id`, 
`home_id`, 
`id_metro`, 
`name_metro`, 
`min_user`, 
`goby`, 
`price`, 
`currency`, 
`prepay`, 
`zalog`, 
`for_days`, 
`d_price`, 
`d_currency`, 
`d_comment`, 
`floor`, 
`floorn`, 
`mebel`, 
`mebel_k`, 
`phone`, 
`tv`, 
`rfgr`, 
`wm`, 
`balkon`, 
`pet`, 
`kids`, 
`comment_client`, `comment_site`, `keepday`, `for_all`, `contact_telefon1`, 
`contact_name1`, `contact_telefon2`, `contact_name2`, `datetime`, `id_phone`, 
`act`) 

VALUES 

( 
" . time() . ",
GeomFromText('POINT(" . $data['yandex_point'] . ")',0) , 
'" . $data['root'] . "', 
'" . $data['kitchen'] . "', 
'" . $data['area'] . "', 
'" . $data['l_area'] . "', 
'" . $data['t_area'] . "', 
'" . $data['adress_name'] . "', 
'" . $data['region_names'] . "',
'" . $data['region_id'] . "', 
'" . $data['city_name'] . "', 
'" . $data['city_id'] . "', 

'" . $data['street_name'] . "', 
'" . $data['street_id'] . "',  
'" . $data['home_id'] . "', 

'" . $data['id_metro'] . "', 
'" . $data['name_metro'] . "', 


'" . $data['min_user'] . "', 
'" . $data['goby'] . "', 
'" . $data['price'] . "', 
'" . $data['currency'] . "' , 

'" . $data['prepay'] . "', 
'" . $data['zalog'] . "', 
'" . $data['for_days'] . "',

'" . $data['d_price'] . "', 
'" . $data['d_currency'] . "', 

'" . $data['d_comment'] . "',
'" . $data['floor'] . "', 
'" . $data['floorn'] . "',

'" . $data['mebel'] . "',
'" . $data['mebel_k'] . "',

'" . $data['phone'] . "', 
'" . $data['tv'] . "', 
'" . $data['rfgr'] . "', 
'" . $data['wm'] . "', 

'" . $data['balkon'] . "', 
'" . $data['pet'] . "', 
'" . $data['kids'] . "', 
	 
'" . $data['comment_client'] . "',
'" . $data['comment_site'] . "',
 '" . $data['keepday'] . "', 
 '" . $data['for_all'] . "', 
 '" . $data['contact_telefon1'] . "',
 '" . $data['contact_name1'] . "', 
 
'" . $data['contact_telefon2'] . "', 
'" . $data['contact_name2'] . "', 
'" . $data['datetime'] . "', 
'" . $data['id_phone'] . "', 
'" . $data['act'] . "')";

        $DB2 -> query($select);
        $data['id_adsent'] = $DB2 -> insert_id();
        // добавляем в общую базу
        $this -> insert_adsent($data);
        return $data['id_adsent'];
    }

    /**
     * Добавление нового объявления в базу о аренде дома , таунхауса или участка
     *
     * @param array $data массим с данными для добавление в базу
     * @return int $id уникальный идентификатор объявления
     * @author
     */
    public function rent_suburbian($data) {

        $DB2 = $this -> load -> database('adsent', TRUE, TRUE);

        $data['datetime'] = date("Y-m-d H:i:s", time());
        $data['id_phone'] = $this -> id_phone;
        $data['act'] = '1';

        // узнаём точное местоположение объёкта
        if (isset($data["yandex_point"])) {
            if (strlen($data["yandex_point"]) < 10) {
                $data["yandex_point"] = $this -> yandex($data["adress_name"]);
            } else {
                $data["yandex_point"] = $data["yandex_point"];
            };
        } else {

            $data["yandex_point"] = $this -> yandex($data["adress_name"]);

        }

        $data['name_type'] = 'rsuburbian';
        if ($data['city_id'] == '7700000000000' || $data['city_id'] == '7800000000000') {
            $data['region_id'] = $data['city_id'];
        }

        $select = "INSERT INTO `rsuburbian` (
                `adress_name`, 
                `region_names`, 
                `region_id`, 
                `city_name`,
                `city_id`, 
                `street_name`, 
                `street_id`, 
                `home_id`, 
                `id_mkad`, 
                `id_metro`, 
                `name_metro`, 
                `from_mkad`, 
                `price`, 
                `currency`, 
                `for_days`, 
                `d_price`, 
                `d_currency`, 
                `newtime`, 
                `d_comment`, 
                `object_type`, 
                `area`, 
                `floorn`, 
                `site`, 
                `unit`, 
                `comment_client`, `comment_site`, `keepday`, `for_all`, 
                `contact_telefon1`, 
                `contact_name1`, `contact_telefon2`, `contact_name2`, 
                `datetime`, 
                `id_phone`, 
                `act`,
                `points`
                ) 
                VALUES 
                (
                '" . $data['adress_name'] . "',
                '" . $data['region_names'] . "',
                '" . $data['region_id'] . "',
                '" . $data['city_name'] . "',
                '" . $data['city_id'] . "',
                '" . $data['street_name'] . "',
                '" . $data['street_id'] . "',
                '" . $data['home_id'] . "',
                '" . $data['id_mkad'] . "', 
                '" . $data['id_metro'] . "', 
                '" . $data['name_metro'] . "', 
                '" . $data['from_mkad'] . "', 
                '" . $data['price'] . "', 
                
                
                '" . $data['currency'] . "', 
                '" . $data['for_days'] . "',
                
                 '" . $data['d_price'] . "',
                 '" . $data['d_currency'] . "',
                 " . time() . ", 
                '" . $data['d_comment'] . "', 
                 '" . $data['object_type'] . "', 
                 '" . $data['area'] . "', 
                 '" . $data['floorn'] . "', 
                 '" . $data['site'] . "', 
                 '" . $data['unit'] . "', 
                 
                 '" . $data['comment_client'] . "', 
                 
                  '" . $data['comment_site'] . "',
                   '" . $data['keepday'] . "',
                   '" . $data['for_all'] . "',
                   
                    
                  '" . $data['contact_telefon1'] . "', 
                  '" . $data['contact_name1'] . "', 
                  '" . $data['contact_telefon2'] . "', 
                  '" . $data['contact_name2'] . "',
                  
                   
                  '" . $data['datetime'] . "', 
                   '" . $data['id_phone'] . "', 
                  '" . $data['act'] . "',
                  GeomFromText('POINT(" . $data['yandex_point'] . ")',0) 
                  );";

        $DB2 -> query($select);
        $data['id_adsent'] = $DB2 -> insert_id();
        // добавляем в общую базу
        $this -> insert_adsent($data);
        return $data['id_adsent'];
    }

    /**
     * Добавление нового объявления в базу о аренде дома , таунхауса или участка
     *
     * @param array $data массим с данными для добавление в базу
     * @return int $id уникальный идентификатор объявления
     * @author
     */
    public function rent_roffice($data) {
        $DB2 = $this -> load -> database('adsent', TRUE, TRUE);

        $data['datetime'] = date("Y-m-d H:i:s", time());
        $data['id_phone'] = $this -> id_phone;

        $data['act'] = '1';

        $data['name_type'] = 'roffice';
        if ($data['city_id'] == '7700000000000' || $data['city_id'] == '7800000000000') {
            $data['region_id'] = $data['city_id'];
        }

        // узнаём точное местоположение объёкта
        if (isset($data["yandex_point"])) {
            if (strlen($data["yandex_point"]) < 10) {
                $data["yandex_point"] = $this -> yandex($data["adress_name"]);
            } else {
                $data["yandex_point"] = $data["yandex_point"];
            };
        } else {

            $data["yandex_point"] = $this -> yandex($data["adress_name"]);

        }

        $select = "INSERT INTO `roffice` 
    (
    `adress_name`, 
    `region_names`, 
    `region_id`, 
    `city_name`, 
    `city_id`, 
    `street_name`, 
    `street_id`, 
    `newtime`, 
    
    
    `home_id`, 
    `id_metro`, 
    
    `name_metro`, 
    `min_user`, 
    `price`, 
    `currency`, 
    `price_type`, 
    `contract`, 
    `floor`, 
    `floorn`, 
    `building`, 
    `enter`, 
    `object_type`, 
    `min_area`, 
    
    
    
    `room`, 
    `r_area`, 
    `phone`, 
    `add_phones`, 
    `mebel`, 
    `share_area`, 
    `t_area`, 
    
    
    
    `comment_client`, 
    `comment_site`, 
    `keepday`, 
    `for_all`, 
    `contact_telefon1`,
    
    
    
     `contact_name1`, 
    `contact_telefon2`, `contact_name2`, `datetime`, `id_phone`, `act`,`points`) 
    
    VALUES 
    
    (
     
    '" . $data["adress_name"] . "', 
    '" . $data["region_names"] . "', 
    '" . $data["region_id"] . "', 
    '" . $data["city_name"] . "', 
    '" . $data["city_id"] . "', 
    
    '" . $data["street_name"] . "', 
    '" . $data["street_id"] . "',
    
     
" . time() . ", 
      '" . $data["home_id"] . "',
      
      '" . $data["id_metro"] . "', 
      '" . $data["name_metro"] . "', 
      
	  
      '" . $data["min_user"] . "', 	
      '" . $data["price"] . "',
      
      '" . $data["currency"] . "',  
      '" . $data["price_type"] . "',  
      '" . $data["contract"] . "',  
      '" . $data["floor"] . "', 
      '" . $data["floorn"] . "', 
      
      
      '" . $data["building"] . "',  
      '" . $data["enter"] . "',
      '" . $data["object_type"] . "',
      '" . $data["min_area"] . "',  
      '" . $data["room"] . "',   
      
      
      
         '" . $data["r_area"] . "',   
        '" . $data["phone"] . "',   
         '" . $data["add_phones"] . "',   
          '" . $data["mebel"] . "',   
          '" . $data["share_area"] . "',  
          '" . $data["t_area"] . "',   
    
	
	
	
       '" . $data["comment_client"] . "',  
        '" . $data["comment_site"] . "',  
          '" . $data["keepday"] . "', 
           '" . $data["for_all"] . "', 
          '" . $data["contact_telefon1"] . "', 
     
     
	 
	 
	   '" . $data["contact_name1"] . "', 
	   '" . $data["contact_telefon2"] . "', 
	   '" . $data["contact_name2"] . "',  
	    '" . $data["datetime"] . "',  
  '" . $data["id_phone"] . "',  
  '" . $data["act"] . "', 
  GeomFromText('POINT(" . $data['yandex_point'] . ")',0) );";

        $DB2 -> query($select);
        $data['id_adsent'] = $DB2 -> insert_id();
        // добавляем в общую базу
        $this -> insert_adsent($data);
        return $data['id_adsent'];
    }

    /**
     * Добавление нового объявления в базу продажа квартир и комнат
     *
     * @param array $data массим с данными для добавление в базу
     * @return int $id уникальный идентификатор объявления
     * @author
     */
    public function sale_flat($data) {
        $DB2 = $this -> load -> database('adsent', TRUE, TRUE);
        $data['name_type'] = 'sflat';
        $data['id_phone'] = '0';

        if ($data['city_id'] == '7700000000000' || $data['city_id'] == '7800000000000') {
            $data['region_id'] = $data['city_id'];
        }

        // узнаём точное местоположение объёкта
        if (isset($data["yandex_point"])) {
            if (strlen($data["yandex_point"]) < 10) {
                $data["yandex_point"] = $this -> yandex($data["adress_name"]);
            } else {
                $data["yandex_point"] = $data["yandex_point"];
            };
        } else {
            $data["yandex_point"] = $this -> yandex($data["adress_name"]);
        }

        $data['datetime'] = date("Y-m-d H:i:s", time());
        $data['id_phone'] = $this -> id_phone;
        $data['act'] = '1';

        $select = "INSERT INTO `sflat` 
		(`room`, `kitchen`, `loggia`, 
		`su_r`, `su_s`, `windows_type`, 
		`object_type`, `sost_type`, `area`, 
		`l_area`, `t_area`, `adress_name`, 
		`region_names`, `region_id`, 
		
		
		
		
		
		
		
		`city_name`, `city_id`, 
		`street_name`, `street_id`, `home_id`, 
		`id_metro`, 
		
		
		`name_metro`, `min_user`, 
		`goby`, `lift`, `g_lift`, `price`, 
		`currency`, `floor`, `floorn`, 
		
		
		
		`phone`, `balkon`, `comment_client`, 
		`comment_site`, `keepday`, `for_all`, 
		`contact_telefon1`, `contact_name1`, 
		`contact_telefon2`, `contact_name2`, 
		`datetime`, `id_phone`, `act`, `newtime`, `points`
		)
		 VALUES (
		 '" . $data['room'] . "', '" . $data['kitchen'] . "', '" . $data['loggia'] . "', 
		 '" . $data['su_r'] . "', '" . $data['su_s'] . "', '" . $data['windows_type'] . "', 
		 '" . $data['object_type'] . "', '" . $data['sost_type'] . "', '" . $data['area'] . "', 
		 '" . $data['l_area'] . "', '" . $data['t_area'] . "', '" . $data['adress_name'] . "', 
		'" . $data['region_names'] . "', '" . $data['region_id'] . "', 
		
		
		
		
		
		
		
		'" . $data['city_name'] . "', '" . $data['city_id'] . "', 
		'" . $data['street_name'] . "', '" . $data['street_id'] . "', 
		 '" . $data['home_id'] . "','" . $data['id_metro'] . "',
		 
		  '" . $data['name_metro'] . "','" . $data['min_user'] . "', 
		  '" . $data['goby'] . "', '" . $data['lift'] . "',
		  
		  
		  '" . $data['g_lift'] . "', '" . $data['price'] . "',
		  
		  '" . $data['currency'] . "', '" . $data['floor'] . "','" . $data['floorn'] . "',
		  
		  
		  '" . $data['phone'] . "', '" . $data['balkon'] . "',
		   '" . $data['comment_client'] . "', 
		'" . $data['comment_site'] . "',
		 
		  '" . $data['keepday'] . "',  '" . $data['for_all'] . "', 
		  '" . $data['contact_telefon1'] . "', 
		  '" . $data['contact_name1'] . "', 
		  '" . $data['contact_telefon2'] . "', 
		 '" . $data['contact_name2'] . "', 
		  '" . $data['datetime'] . "', 
		 '" . $data['id_phone'] . "',
		 '" . $data['act'] . "',
		 '" . time() . "', 
  GeomFromText('POINT(" . $data['yandex_point'] . ")',0)
		 );";

        $DB2 -> query($select);
        $data['id_adsent'] = $DB2 -> insert_id();
        // добавляем в общую базу
        $this -> insert_adsent($data);
        return $data['id_adsent'];
    }

    /**
     * Добавление нового объявления в базу продажа дома и участка
     *
     * @param array $data массим с данными для добавление в базу
     * @return int $id уникальный идентификатор объявления
     * @author
     */
    public function sale_suburbian($data) {
        $DB2 = $this -> load -> database('adsent', TRUE, TRUE);

        $data['datetime'] = date("Y-m-d H:i:s", time());
        $data['id_phone'] = $this -> id_phone;
        $data['act'] = '1';
        $data['name_type'] = 'ssuburbian';

        if ($data['city_id'] == '7700000000000' || $data['city_id'] == '7800000000000') {
            $data['region_id'] = $data['city_id'];
        }
        // узнаём точное местоположение объёкта
        if (isset($data["yandex_point"])) {
            if (strlen($data["yandex_point"]) < 10) {
                $data["yandex_point"] = $this -> yandex($data["adress_name"]);
            } else {
                $data["yandex_point"] = $data["yandex_point"];
            };
        } else {

            $data["yandex_point"] = $this -> yandex($data["adress_name"]);

        }

        $select = "INSERT INTO `ssuburbian` 
(`adress_name`, `region_names`, `region_id`, `city_name`, `city_id`,
 `street_name`, `street_id`, `home_id`, `id_metro`, `name_metro`, 
 `object_type`, `area`, `floorn`, `site`, `unit`, 
`land_type`, `price`, `currency`, `price_type`, `newtime`, 
`comment_client`, `comment_site`, `keepday`, `contact_telefon1`, `contact_name1`, 
`contact_telefon2`, `contact_name2`, `datetime`, `id_phone`, `act`) 

VALUES 
 
('" . $data['adress_name'] . "', '" . $data['region_names'] . "', '" . $data['region_id'] . "', '" . $data['city_name'] . "', '" . $data['city_id'] . "',
'" . $data['street_name'] . "', '" . $data['street_id'] . "', '" . $data['home_id'] . "', '" . $data['id_metro'] . "', '" . $data['name_metro'] . "', 
'" . $data['object_type'] . "', '" . $data['area'] . "', '" . $data['floorn'] . "', '" . $data['site'] . "', '" . $data['unit'] . "', 
'" . $data['land_type'] . "', '" . $data['price'] . "', '" . $data['currency'] . "', '" . $data['price_type'] . "', " . time() . ", 
'" . $data['comment_client'] . "','" . $data['comment_site'] . "',  '" . $data['keepday'] . "', '" . $data['contact_telefon1'] . "', '" . $data['contact_name1'] . "', 
'" . $data['contact_telefon2'] . "', '" . $data['contact_name2'] . "','" . $data['datetime'] . "','" . $data['id_phone'] . "','" . $data['act'] . "');";

        $DB2 -> query($select);
        $data['id_adsent'] = $DB2 -> insert_id();
        // добавляем в общую базу
        $this -> insert_adsent($data);
        return $data['id_adsent'];
    }

    /**
     * Добавление нового объявления в базу продажа офиса, нежилого помещения
     *
     * @param array $data массим с данными для добавление в базу
     * @return int $id уникальный идентификатор объявления
     * @author
     */
    public function sale_office($data) {
        $newtime = time();

        $DB2 = $this -> load -> database('adsent', TRUE, TRUE);

        $data['id_phone'] = $this -> id_phone;
        $data['act'] = '1';
        $data['datetime'] = date("Y-m-d H:i:s", time());

        $data['name_type'] = 'soffice';

        $adsent['datetime'] = $data['datetime'];
        if ($data['city_id'] == '7700000000000' || $data['city_id'] == '7800000000000') {
            $data['region_id'] = $data['city_id'];
        }
        $data['dt'] = $dt;

        // узнаём точное местоположение объёкта
        if (isset($data["yandex_point"])) {
            if (strlen($data["yandex_point"]) < 10) {
                $data["yandex_point"] = $this -> yandex($data["adress_name"]);
            } else {
                $data["yandex_point"] = $data["yandex_point"];
            };
        } else {

            $data["yandex_point"] = $this -> yandex($data["adress_name"]);

        }

        $select = "INSERT INTO `soffice` (`adress_name`, `region_names`, `region_id`, 
       `city_name`, `city_id`, `street_name`, `street_id`,
       
	    `home_id`, `id_metro`, `name_metro`,
	     
       `min_user`, 
       `price`, `currency`, 
       
       
       `price_type`, `contract`, `floor`, `floorn`, `newtime`, 
       `building`,        `enter`, 
       `object_type`, 
       `min_area`, `room`, `r_area`, `phone`, `add_phones`, 
       `mebel`, `share_area`, `t_area`, `comment_client`, `comment_site`, `keepday`, 
       
       
       `for_all`, `contact_telefon1`, 
       `contact_name1`, `contact_telefon2`, `contact_name2`, `id_phone`, 
       `act`, `datetime`, `points`) VALUES 

('" . $data['adress_name'] . "', 
'" . $data['region_names'] . "', '" . $data['region_id'] . "','" . $data['city_name'] . "','" . $data['city_id'] . "','" . $data['street_name'] . "',
'" . $data['street_id'] . "', '" . $data['home_id'] . "',  '" . $data['id_metro'] . "',  '" . $data['name_metro'] . "',  '" . $data['min_user'] . "',
  '" . $data['price'] . "',  '" . $data['currency'] . "',  '" . $data['price_type'] . "',  '" . $data['contract'] . "',  '" . $data['floor'] . "',
    '" . $data['floorn'] . "',  '" . time() . "',  '" . $data['building'] . "',  '" . $data['enter'] . "',  '" . $data['object_type'] . "',
  '" . $data['min_area'] . "',  '" . $data['room'] . "',  '" . $data['r_area'] . "',  '" . $data['phone'] . "',  '" . $data['add_phones'] . "',  
  '" . $data['mebel'] . "',  '" . $data['share_area'] . "',  '" . $data['t_area'] . "',  '" . $data['comment_client'] . "', '" . $data['comment_site'] . "',  
 '" . $data['keepday'] . "',  '" . $data['for_all'] . "',  '" . $data['contact_telefon1'] . "',  '" . $data['contact_name1'] . "',  '" . $data['contact_telefon2'] . "', 
 '" . $data['contact_name2'] . "',  '" . $data['id_phone'] . "', '" . $data['act'] . "',  '" . $data['datetime'] . "', 	GeomFromText('POINT(" . $data['yandex_point'] . ")',0)
 )";

        $DB2 -> query($select);
        $data['id_adsent'] = $DB2 -> insert_id();
        // добавляем в общую базу
        $this -> insert_adsent($data);
        return $data['id_adsent'];
    }

    /**
     * Получаем координаты адреса по заданому адресу
     *
     * @param char $data адрес
     * @return char
     * @author
     */
    public function yandex($adress) {

        # http://geocode-maps.yandex.ru/1.x/?geocode=Москва , Улица Бирюлевская , 13к3

        $arr = false;

        # &locality=москва
        #       sll 37.643695000000015,55.80424550197424
        $format = '&format=json';
        $this -> key = '&key=ADvWGFABAAAASwkMTwIAsHoMAnDWW80CWMZnXrEB0hgKksQAAAAAAAAAAABBWwFnq2ESo-cr7ggavsWSyhVomg==';

        # http://geocode-maps.yandex.ru/1.x/?key=ADvWGFABAAAASwkMTwIAsHoMAnDWW80CWMZnXrEB0hgKksQAAAAAAAAAAABBWwFnq2ESo-cr7ggavsWSyhVomg==

        $dop = 'geocode=' . $adress . $this -> key;

        $url = 'http://geocode-maps.yandex.ru/1.x/';

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        // set url to post to
        curl_setopt($ch, CURLOPT_FAILONERROR, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        // allow redirects
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // return into a variable
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        // times out after 4s
        curl_setopt($ch, CURLOPT_POST, 1);
        // set POST method
        curl_setopt($ch, CURLOPT_POSTFIELDS, $dop);
        // add POST fields
        $result = curl_exec($ch);
        // run the whole process
        curl_close($ch);

        # print_r($result);

        $xml = new SimpleXMLElement($result);

        $pattern = "|<lowerCorner>(.*?)</lowerCorner>|is";
        preg_match_all($pattern, $result, $out);

        #  print_r($out[1][0]);
        #   echo $out[1][0];

        $code = '';
        #			echo json_encode($result);

        return $out[1][0];
    }

    public function insert_adsent($data) {
        $DB2 = $this -> load -> database('adsent', TRUE, TRUE);
        $select = "INSERT INTO 
`adsent`(
`newtime`,`id_metro`,
`name_metro`,
`id_adsent`, 
`name_type`,
`region_id`,
`region_names`,
`city_id`,
`city_name`,
`street_id`,
`street_name`,
`contact_telefon1`,
`contact_name1`,
`contact_telefon2`,
`contact_name2`,
`id_phone`, 
`datetime`,
`points`
) 
VALUES ( 
 " . time() . " , 
'" . $data['id_metro'] . "',
'" . $data['name_metro'] . "',
'" . $data['id_adsent'] . "', 
'" . $data['name_type'] . "',
'" . $data['region_id'] . "',
'" . $data['region_names'] . "',
'" . $data['city_id'] . "',
'" . $data['city_name'] . "',
'" . $data['street_id'] . "',
'" . $data['street_name'] . "',
'" . $data['contact_telefon1'] . "',
'" . $data['contact_name1'] . "',
'" . $data['contact_telefon2'] . "',
'" . $data['contact_name2'] . "',
'" . $data['id_phone'] . "',
'" . $data['datetime'] . "',
 GeomFromText('POINT(" . $data["yandex_point"] . ")',0));";
        $DB2 -> query($select);
        $id = $DB2 -> insert_id();
        return $id;
    }

}
